import javax.management.relation.RelationNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        String filePath = "src/demo01.txt";

        String filePath2 = "src/demo02.txt";

        String filePath3 = "src/student.txt";

        FileReader fileReader = null;

        FileWriter fileWriter = null;

        fileWriter = new FileWriter(filePath);

        fileWriter.write("hello my name is yunding");

        fileWriter.flush();

        System.out.println("写入完成！");

        int date = 0;

        int num1 = 0, num2 = 0;

        fileReader = new FileReader(filePath);

        fileWriter = new FileWriter(filePath2);

        while ((date = fileReader.read()) != -1) {

            char idx = (char) date;

            if (Objects.equals(idx, 'e')) {

                num1++;

                fileWriter.write(idx);
            }if(Objects.equals(idx, 'a')){

                num2++;

                fileWriter.write(idx);
            }
            fileWriter.write(idx);
        }

        System.out.println("拷贝完成！");
        
        System.out.println("e的出现次数是" + num1);

        System.out.println("a的出现次数是" + num2);

        fileWriter.flush();

        Boolean flag = true;

        while (flag) {

            fileWriter = new FileWriter(filePath3, true);

            System.out.println("请输入你要添加的学生姓名 班级 年龄");

            Scanner sc = new Scanner(System.in);

            String info = sc.nextLine();

            fileWriter.write(info);

            fileWriter.write("\r\n");

            System.out.println("停止添加请按1, 按任意键以继续！");

            int i = sc.nextInt();

            if(i == 1){
                flag = false;
            }
            fileWriter.flush();
        }
        fileWriter.close();
        fileReader.close();

    }
}